package main

import (
	"encoding/json"
	"fmt"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
	"math/big"
)

var logger = shim.NewLogger("trans_cc")

type TransInfo struct {
	BankCardNo  string
	CusName     string
	IdNo        string
	MobilePhone string
	Amount      big.Float
	CusNo       string
}

//1 创建结构体
type SimpleChaincode struct {
}

//2 创建init函数
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
	logger.Info("########### trans_cc 初始化 ###########")

	return shim.Success(nil)
}

//3 创建调用函数
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	logger.Info("########### trans_cc Invoke ###########")

	function, args := stub.GetFunctionAndParameters()

	logger.Info("接收参数function=" + function)
	logger.Info("接收参数为" + args[0])

	if function == "save" {
		// Deletes an entity from its state
		return t.save(stub, args)
	}

	if function == "delete" {
		// Deletes an entity from its state
		return t.delete(stub, args)
	}

	if function == "query" {
		// queries an entity state
		return t.query(stub, args)
	}

	logger.Errorf("未知操作,可以执行的操作有 'delete', 'query', or 'save'. 现在收到参数为: %v", args[0])
	return shim.Error(fmt.Sprintf("未知操作,可以执行的操作有 'delete', 'query', or 'save'. 现在收到参数为: %v", args[0]))

}

func (t *SimpleChaincode) save(stub shim.ChaincodeStubInterface, args []string) pb.Response {

	//定义实体
	var Trans TransInfo
	var Error error

	if len(args) != 1 {
		return shim.Error("参数不匹配,期望参数个数为1")
	}

	//logger.Info("接收到参数为:", args[0])

	//var json = jsoniter.ConfigCompatibleWithStandardLibrary
	Error = json.Unmarshal([]byte(args[0]), &Trans)
	//Trans.CusNo = "0123456"
	//Trans.Amount.SetFloat64(2.22)
	if Error != nil {
		logger.Error("json", Error)
		return shim.Error("Json格式解析错误,请确认Json数据格式")
	}

	Error = stub.PutPrivateData("COLLECTION_FOR_TRANS", Trans.CusNo, []byte(args[0]))

	if Error != nil {
		logger.Error("save", Error)
		return shim.Error("保存交易信息异常!" + Error.Error())
	}

	if transientMap, Error := stub.GetTransient(); Error == nil {
		if transientData, ok := transientMap["event"]; ok {
			stub.SetEvent("event", transientData)
		}
		if transientData, ok := transientMap["result"]; ok {
			return shim.Success(transientData)
		}
	}

	logger.Info("保存成功!")
	return shim.Success(nil)
}
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args) != 1 {
		return shim.Error("参数不匹配,期望参数个数为1")
	}
	CusNo := args[0]
	e := stub.DelPrivateData("COLLECTION_FOR_TRANS", CusNo)

	if e != nil {
		return shim.Error("删除交易信息异常!" + e.Error())
	}

	return shim.Success(nil)
}
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {

	if len(args) != 1 {
		return shim.Error("参数不匹配,期望参数个数为1")
	}

	CusNo := args[0]
	TransBytes, e := stub.GetPrivateData("COLLECTION_FOR_TRANS", CusNo)

	if e != nil {
		return shim.Error("查询数据异常!" + e.Error())
	}

	if TransBytes == nil {
		return shim.Error("未查询到数据!")
	}
	logger.Info("查询到数据:" + string(TransBytes))
	return shim.Success(TransBytes)

}

func main() {
	err := shim.Start(new(SimpleChaincode))
	if err != nil {
		logger.Errorf("Error starting Simple chaincode: %s", err)
	}

}
